/* Filename: mysu.c
 * Description:
 * $ su root
 * $ chown root mysu
 * $ chmod u+s mysu
 * $ exit
 * $ ./mysu 0 cat /etc/shadow
 * Last modified: humble 20200323 21:33
 */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

static void usage(void);

int main(int argc, char **argv)
{
    pid_t pid;

    if(argc < 3){
        fprintf(stderr, "argc < 3");
        usage();
        exit(1);
    }

    pid = fork();
    if(pid < 0){
        perror("fork()");
        exit(1);
    }
    else if(pid == 0){
        setuid(atoi(argv[1]));
        execvp(argv[2], argv+2);
        perror("execvp()");
        exit(1);
    }

    wait(NULL);


    return 0;
}

static void usage(void)
{
#define USAGE "Usage:\n\
  Helloworld\n"
    printf(USAGE);
}
